//
//  WBVisitorView.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/6.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit

protocol WBVisitorViewDelegate: NSObjectProtocol {
    func login()
}

class WBVisitorView: UIView {
    /// swift中添加代理属性的写法
    weak var delegate: WBVisitorViewDelegate?
    
    /// 标题和图标的参数字典
    var visitorInfo: [String: Any]? {
        didSet {
            //设置图标
            let imageName = visitorInfo?["imageName"] as! String
            
            iconImageView.image = UIImage(named: imageName)
            
            //标题
            titleLable.text = visitorInfo?["message"] as? String
            
            //动画设置
            if let _ = visitorInfo?["isAnimation"] {
                //说明需要加动画
                circleImageView.isHidden = false
            }
        }
    }
    
    /// 主图标
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView(imageName: "visitordiscover_feed_image_house")
        return imageView
    }()
    
    /// 菊花
    lazy var circleImageView: UIImageView = {
        let imageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
        return imageView
    }()
    
    /// 遮罩的图片
    lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    /// 文字label
    lazy var titleLable: UILabel = UILabel(text: "来这里看看有什么惊喜", alignment: .center)
    
    /// 注册按钮
    lazy var registButton: UIButton = UIButton(text: "注册", titleColor: UIColor.orange,bgImage: "common_button_white_disable", target: self, selector: #selector(login))
    
    /// 登录的按钮
    lazy var loginButton: UIButton = UIButton(text: "登录", bgImage: "common_button_white_disable", target: self, selector: #selector(login))

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 设置UI
extension WBVisitorView {
    func setupUI () {
        self.backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)
        
        //1. 添加子控件
        addSubview(iconImageView)
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(titleLable)
        addSubview(registButton)
        addSubview(loginButton)
        
        circleImageView.isHidden = true
        
        //2. 设置子控件
        //添加动画,keyPath决定了动画的类型
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2.0 * .pi //360度旋转
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false //当页面切换后，回来继续转动
        
        circleImageView.layer.add(animation, forKey: nil)
        
        //3. 自动布局
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-100)
        }
        
        circleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(iconImageView)
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-70)
            make.left.right.equalTo(self)
        }
        
        titleLable.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(iconImageView).offset(150)
            make.left.equalTo(self).offset(80)
            make.right.equalTo(self).offset(-80)
        }
        
        registButton.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(80)
            make.bottom.equalTo(self).offset(-150)
            make.size.equalTo(CGSize(width: 80, height: 30))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-80)
            make.bottom.equalTo(self).offset(-150)
            make.size.equalTo(CGSize(width: 80, height: 30))
        }
    }
}

// MARK: - 事件
extension WBVisitorView {
    @objc func login() {
        delegate?.login()
    }
}
